/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-09-18
 * Time:0:03
 */

/**
 * 懒汉模式
 * 1.先给单例模式为空
 * 2.等到调用时再给初始化
 */
class SingletonLazy{
    //volatile:防止指令重排序,内存可见性问题
    private volatile static Singleton instance = null;

    public static Singleton getInstance(){
        //这里的if是为了防止多次加锁
        if (instance == null){
            //加锁
            synchronized (SingletonLazy.class){
                if (instance == null){
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
    //单例模式,防止多次实例化
    public SingletonLazy(){

    }
}


public class Singleton {

}
